﻿package org.edu.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.edu.bean.Role;
import org.edu.bean.Role_bar;
import org.edu.bean.User;
import org.edu.bean._navigationbar;
import org.edu.service.NavigationBarService;
import org.edu.service.RoleService;

import com.opensymphony.xwork2.ActionSupport;

public class BarController extends ActionSupport {
	@Resource
	NavigationBarService navigationBarService ;
	@Resource
	RoleService roleService ;
	
	private String navigationbar ;
	private List<List> barList ;
	private int roleId ;
	private Role role;
	private String barStr;
	private String result;
	private String roleBarVal;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public String bar_navi(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession() ;
		User user = (User)session.getAttribute("user");
		List<Role> roles = roleService.selectRole_User(user);
		List<_navigationbar> _Navigationbars = navigationBarService.firstBar(roles);
		session.setAttribute("_Navigationbars", _Navigationbars);
		session.setAttribute("roles", roles);
		return SUCCESS ;
	}
	
	@SuppressWarnings("unchecked")
	public String _a_bBar(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession() ;
		if(navigationbar!=null){
			String n_id  = navigationbar ;
			if(n_id.length()<2){
				n_id ="0"+n_id;
			}
			List li = (List)session.getAttribute("roles");
			List<List> list = navigationBarService.secend_threeBar((List)session.getAttribute("roles"),n_id );
			session.setAttribute("_a_b_bars", list);
		}
		return SUCCESS ;
	}
	public String barList(){
		barList = navigationBarService.barLists();
		if(roleId != 0){
			role = roleService.getRoleById(roleId);
		}
		return SUCCESS ;
	}
	
	@SuppressWarnings("unchecked")
	public String selectcheckbox(){
		boolean status = navigationBarService.selectRoleBarByRoleIdBarVal(barStr, roleId);
		Map map = new HashMap();
        map.put("status", status);
		JSONObject  object = JSONObject.fromObject(map);
		result = object.toString();
		return SUCCESS ;
	}
	
	/**
	 * @exception 修改的时候 删除 roleBar；
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String editDelRoleBar(){
		Role_bar roleBar = navigationBarService.selectRoleBarByroleidbarid(roleBarVal, roleId);
		boolean status = navigationBarService.delRoleBar(roleBar);
		Map map = new HashMap();  
        map.put("status", status);
		JSONObject  object = JSONObject.fromObject(map);
		result = object.toString();
		return SUCCESS;
	}
	/**
	 * 
	 * @exception 修改的时候添加roleBar ;
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String editAddRoleBar(){
		Role_bar roleBar = new Role_bar() ;
		roleBar.setBarid(roleBarVal);
		roleBar.setRoleid(roleId);
		boolean status = false ;
		int roleid = navigationBarService.createRoleBar(roleBar) ;
		if(roleid>0){
			status = true;
		}
		Map map = new HashMap();
        map.put("status", status);
		JSONObject  object = JSONObject.fromObject(map);
		result = object.toString();
		return SUCCESS;
	}
	public String getNavigationbar() {
		return navigationbar;
	}

	public void setNavigationbar(String navigationbar) {
		this.navigationbar = navigationbar;
	}

	public List<List> getBarList() {
		return barList;
	}

	public void setBarList(List<List> barList) {
		this.barList = barList;
	}

	

	public int getRoleId() {
		return roleId;
	}

	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public String getBarStr() {
		return barStr;
	}

	public void setBarStr(String barStr) {
		this.barStr = barStr;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}


	public String getRoleBarVal() {
		return roleBarVal;
	}

	public void setRoleBarVal(String roleBarVal) {
		this.roleBarVal = roleBarVal;
	}

	
}
